package com.icode.service.impl.house;

import com.icode.core.dto.house.HomePageDTO;
import com.icode.core.model.house.*;
import com.icode.core.shared.DataToJson;
import com.icode.dao.HomePageDao;
import com.icode.service.house.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-2
 * Time: 下午2:54
 * To change this template use File | Settings | File Templates.
 */
@Service("homePageService")
@Transactional
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private HomePageDao homePageDao;

    @Override
    @Transactional
    public HomePageDTO findHomePage() {
        List<HomePage> homePages = homePageDao.findAll(HomePage.class);
        HomePageDTO homePageDTO = new HomePageDTO();
        if(!homePages.isEmpty()){
            HomePage homePage = homePages.get(0);
            homePageDTO = HomePageDTO.toDTO(homePage);
        }
        return homePageDTO;
    }

    @Override
    @Transactional
    public void updateHomePage(HomePageDTO homePageDTO) {
        HomePage homePage = homePageDTO.toHomePage(homePageDTO);
        if ("".equals(homePageDTO.getId())) {
            homePageDao.saveOrUpdate(homePage);
        } else {
            HomePage update = homePageDao.findByGuid(HomePage.class, homePageDTO.getGuid());
            if (!"".equals(homePageDTO.getIntroImg().get(0).getOriginalFilename())) {
                List<HomeIntroImage> homeIntroImages = homePageDao.findHomeIntroImageByHomePageId(homePageDTO.getId());
                homePageDao.deleteIntroImages(homeIntroImages);
                update.initIntroImg(homePage.introductionImg());
            }
            if (!"".equals(homePageDTO.getInfoImg().get(0).getOriginalFilename())) {
                List<HomeInfoImage> homeInfoImages = homePageDao.findHomeInfoImageByHomePageId(homePageDTO.getId());
                homePageDao.deleteInfoImages(homeInfoImages);
                update.initInfoImg(homePage.infoImg());
            }
            if (!"".equals(homePageDTO.getPartnersImg().get(0).getOriginalFilename())) {
                List<HomePartnerImage> homePartnerImages = homePageDao.findHomePartnersImageByHomePageId(homePageDTO.getId());
                homePageDao.deletePartnersImages(homePartnerImages);
                update.initPartnersImg(homePage.partners());
            }
            update.initHomePage(homePage.title(), homePage.contents());
            homePageDao.saveOrUpdate(update);
        }
    }

    @Override
    @Transactional
    public HomeIntroImage findIntroByImgGuid(String imgGuid) {
        UploadImage uploadId = homePageDao.findByGuid(UploadImage.class, imgGuid);
        HomeIntroImage homeIntroImage = homePageDao.findHomeIntroImageByUploadImage(uploadId);
        return homeIntroImage;
    }

    @Override
    @Transactional
    public HomeInfoImage findInfoByImgGuid(String imgGuid) {
        UploadImage uploadId = homePageDao.findByGuid(UploadImage.class, imgGuid);
        HomeInfoImage homeInfoImage = homePageDao.findHomeInfoImageByUploadImage(uploadId);
        return homeInfoImage;
    }

    @Override
    @Transactional
    public HomePartnerImage findPartnersByImgGuid(String imgGuid) {
        UploadImage uploadId = homePageDao.findByGuid(UploadImage.class, imgGuid);
        HomePartnerImage homePartnerImage = homePageDao.findHomePartnersByUploadImage(uploadId);
        return homePartnerImage;
    }

    @Override
    @Transactional
    public void deleteObject(Object object) {
        homePageDao.deleteObject(object);
    }

    @Override
    @Transactional
    public List<HomeIntroImage> findHomeIntroImageByHomePageId(String homePageId) {
        return homePageDao.findHomeIntroImageByHomePageId(homePageId);
    }

    @Override
    @Transactional
    public List<HomeInfoImage> findHomeInfoImageByHomePageId(String homePageId) {
        return homePageDao.findHomeInfoImageByHomePageId(homePageId);
    }

    @Override
    @Transactional
    public List<HomePartnerImage> findHomePartnerImageByHomePageId(String homePageId) {
        return homePageDao.findHomePartnersImageByHomePageId(homePageId);
    }

    @Override
    @Transactional
    public void updateHomeImagesByType(String type, HttpServletResponse response) {
        HomePage homePage = homePageDao.findHomePage();
        if ("intro".equals(type)) {
            List<HomeIntroImage> homeIntroImages = homePage.introductionImg();
            DataToJson.homeIntroImageToJson(homeIntroImages, response);
        } else if ("info".equals(type)) {
            List<HomeInfoImage> homeInfoImages = homePage.infoImg();
            DataToJson.homeInfoImageToJson(homeInfoImages, response);
        } else if ("partners".equals(type)) {
            List<HomePartnerImage> homePartnerImages = homePage.partners();
            DataToJson.homePartImageToJson(homePartnerImages, response);
        }
    }

}
